/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doa;

import java.util.List;
import models.Admin;
import models.Twitterer;
import org.hibernate.Query;
import org.hibernate.Transaction;

/**
 *
 * @author Misja
 */
public class LoginDAO {

    public static Twitterer getTwittererByNamePassword(String userName, String password) {
        Twitterer twitterer = null;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            Query query = HibernateSessionController.Instance().getHibernateSession().createQuery("from Twitterer where userName = :userName and password =:password");
            query.setParameter("userName", userName);
            query.setParameter("password", password);
            List<Twitterer> result = query.list();
            if (!result.isEmpty()) {
                twitterer = (Twitterer) query.list().get(0);
                tx.commit();
            }
        } catch (Exception e) {
        }
        return twitterer;
    }
    
     public static Admin getAdminByNamePassword(String userName, String password) {
        Admin admin = null;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            Query query = HibernateSessionController.Instance().getHibernateSession().createQuery("from Admin where userName = :userName and password =:password");
            query.setParameter("userName", userName);
            query.setParameter("password", password);
            List<Admin> result = query.list();
            if (!result.isEmpty()) {
                admin = (Admin) query.list().get(0);
                tx.commit();
            }
        } catch (Exception e) {
        }
        return admin;
    }
}
